(function (vc) {

    vc.extends({
        data: {
            addShopRecommendInfo: {
                communityId: '',
                communityName: '',
                shopName: '',
                shopId: '',
                prodId:'',
                recommendType: 'GOODS',
                logo: '',
                prodName: '',
                price: '',
                url: '',
                shops:[],
                products:[],
            }
        },
        _initMethod: function () {
            
        },
        _initEvent: function () {
            vc.on("addShopRecommend", "notifyUploadCoverImage", function (_param) {
                if (_param) {
                    $that.addShopRecommendInfo.logo = _param[0].url;
                } else {
                    $that.addShopRecommendInfo.logo = '';
                }
            });
            vc.on('addShopRecommend', 'openAddShopRecommendModal', function (_param) {
                $that.addShopRecommendInfo.communityId = _param.communityId;
                $that.addShopRecommendInfo.communityName = _param.communityName;
                $that._listAShopRecommendShops();
                vc.emit('addShopRecommend', 'uploadImageUrl', 'notifyPhotos',[]);

                $('#addShopRecommendModel').modal('show');
            });
        },
        methods: {
            addShopRecommendValidate() {
                return vc.validate.validate({
                    addShopRecommendInfo: $that.addShopRecommendInfo
                }, {
                    'addShopRecommendInfo.logo': [
                        {
                            limit: "required",
                            param: "",
                            errInfo: "推荐logo不能为空"
                        },
                        {
                            limit: "maxLength",
                            param: "512",
                            errInfo: "推荐logo不能超过512"
                        },
                    ],
                    'addShopRecommendInfo.prodName': [
                        {
                            limit: "required",
                            param: "",
                            errInfo: "商品名称不能为空"
                        },
                        {
                            limit: "maxLength",
                            param: "256",
                            errInfo: "商品名称不能超过256"
                        },
                    ],
                    'addShopRecommendInfo.price': [
                        {
                            limit: "required",
                            param: "",
                            errInfo: "价格不能为空"
                        },
                        {
                            limit: "maxLength",
                            param: "10",
                            errInfo: "价格不能超过10"
                        },
                    ],
                });
            },
            saveShopRecommendInfo: function () {
                if (!$that.addShopRecommendValidate()) {
                    vc.toast(vc.validate.errInfo);
                    return;
                }
                $that.addShopRecommendInfo.url = '/pages/goods/goods?productId=' + $that.addShopRecommendInfo.prodId + '&shopId=' + $that.addShopRecommendInfo.shopId;
                vc.http.apiPost(
                    '/recommend.saveCommunityRecommend',
                    JSON.stringify($that.addShopRecommendInfo),
                    {
                        emulateJSON: true
                    },
                    function (json, res) {
                        //vm.menus = vm.refreshMenuActive(JSON.parse(json),0);
                        let _json = JSON.parse(json);
                        if (_json.code == 0) {
                            //关闭model
                            $('#addShopRecommendModel').modal('hide');
                            $that.clearAddShopRecommendInfo();
                            vc.emit('communityRecommend', 'listCommunityRecommend', {});
                            return;
                        }
                        vc.toast(_json.msg);

                    },
                    function (errInfo, error) {
                        console.log('请求失败处理');

                        vc.toast(errInfo);

                    });
            },
            clearAddShopRecommendInfo: function () {
                $that.addShopRecommendInfo = {
                    communityId: '',
                    communityName: '',
                    shopName: '',
                    shopId: '',
                    prodId:'',
                    recommendType: 'GOODS',
                    logo: '',
                    prodName: '',
                    price: '',
                    url: '',
                    shops:[],
                    products:[],
                };
            },
            _listAShopRecommendShops: function () {
                let param = {
                    params: {
                        page: 1,
                        row: 100
                    }
                };
                //发送get请求
                vc.http.apiGet('/shop.queryShopsByAdmin',
                    param,
                    function (json, res) {
                        let _json = JSON.parse(json);
                        $that.addShopRecommendInfo.shops = _json.data;
                    },
                    function (errInfo, error) {
                        console.log('请求失败处理');
                    }
                );
            },
            queryAShopGoods:function(){
                let _shops = $that.addShopRecommendInfo.shops;
                let _shopId = $that.addShopRecommendInfo.shopId;
                _shops.forEach(_s => {
                    if(_s.shopId == _shopId){
                        $that.addShopRecommendInfo.shopName = _s.shopName;
                    }
                });
                $that._listAGoodsProducts();
            },
            _listAGoodsProducts: function() {
                let param = {
                    params: {
                        page: 1,
                        row: 500,
                        shopId: $that.addShopRecommendInfo.shopId,
                    }
                };
                //发送get请求
                vc.http.apiGet('/product.queryProduct',
                    param,
                    function(json, res) {
                        let _json = JSON.parse(json);
                        $that.addShopRecommendInfo.products = _json.data;
                    },
                    function(errInfo, error) {
                        console.log('请求失败处理');
                    }
                );
            },
            selectGoods:function(){
                let _products = $that.addShopRecommendInfo.products;
                let _prodId = $that.addShopRecommendInfo.prodId;
                _products.forEach(_p => {
                    if(_p.productId == _prodId){
                        $that.addShopRecommendInfo.prodName = _p.prodName;
                        $that.addShopRecommendInfo.price = _p.price;
                        $that.addShopRecommendInfo.logo = _p.coverPhoto;
                        let _photos = [];
                        _photos.push(_p.coverPhoto);
                       vc.emit('addShopRecommend', 'uploadImageUrl', 'notifyPhotos',_photos);
                    }
                });
            }
        }
    });

})(window.vc);
